package cn.readjava.controller.manage;

import cn.readjava.bean.Article;
import cn.readjava.bean.ArticleCategory;
import cn.readjava.service.ArticleCategoryService;
import cn.readjava.service.ArticleService;
import cn.readjava.util.ResponseResult;
import cn.readjava.vo.ArticleCategoryVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/manage/article")
public class ManageArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleCategoryService articleCategoryService;

    // 删除
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Object deleteArticle(@PathVariable Long id) {
        try {
            articleService.delete(id);
            return new ResponseResult(200, "删除成功", null);
        } catch (Exception e) {
            return new ResponseResult(100, "删除失败", null);
        }
    }

    // 预更新
    @GetMapping("/update/{id}")
    public String updateArticle(@PathVariable Long id, Model model) {
        Article article = articleService.getOne(id);
        model.addAttribute("article", article);
        List<ArticleCategory> articleCategoryList = articleCategoryService.findAll();
        model.addAttribute("articleCategoryList", articleCategoryList);
        return "manage/manage-article-update";
    }

    // 更新
    @PostMapping("/update")
    @ResponseBody
    public Object updateArticle(Article ar) {
        Article article = articleService.save(ar);
        if (article != null) {
            return new ResponseResult(200, "更新成功", article);
        }
        return new ResponseResult(100, "更新失败", null);
    }

    // 预添加
    @GetMapping("/add")
    public String addArticle(Model model) {
        List<ArticleCategory> articleCategoryList = articleCategoryService.findAll();
        model.addAttribute("articleCategoryList", articleCategoryList);
        return "manage/manage-article-add";
    }

    // 添加
    @PostMapping("/add")
    @ResponseBody
    public Object addArticle(Article article) {
        article.setArticleState(0);
        Article ar = articleService.save(article);
        if (ar != null) {
            return new ResponseResult(200, "发布成功", ar);
        }
        return new ResponseResult(100, "发布失败", null);
    }

    // 分类文章
    @GetMapping("/list/{id}")
    public String articleList(@PathVariable Long id, Model model) {
        List<Article> articleList = articleService.findAllByArticleTypeId(id);
        List<ArticleCategory> articleCategoryList = articleCategoryService.findAll();
        List<ArticleCategoryVo> articleCategoryVoList = new ArrayList<>();
        for (Article ar : articleList) {
            for (ArticleCategory ac : articleCategoryList) {
                if (ar.getArticleTypeId() == ac.getAcId()) {
                    articleCategoryVoList.add(new ArticleCategoryVo(ar, ac));
                }
            }
        }
        model.addAttribute("articleCategoryVoList", articleCategoryVoList);
        return "manage/manage-article";
    }
}
